﻿Imports Ecodex.RestAPI
Imports System.Text
Imports System.ServiceModel

Public Class frmDeleteDocument
    Private _random As New Random

    Private Sub btnDeleteDocument_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteDocument.Click
        Dim transactionId As Long = _random.[Next]()
        Dim transactionIdOriginal As Long
        Dim token As String = String.Empty
        Dim Errores As String = String.Empty
        Try
            Using Cancelacion = New SrvTimbrado.TimbradoClient()
                Dim Util As Utilerias = New Utilerias()
                Errores = Util.CreateToken(txtRFC.Text, transactionId, token)
                If (String.IsNullOrEmpty(Errores) = False) Then
                    ShowUnknownError(Errores)
                    Exit Sub
                End If
                Dim respuesta = Cancelacion.CancelaTimbrado(txtRFC.Text, token, transactionIdOriginal, txtUUID.Text)
                txtStatus.Text = respuesta
                txtResultUUID.Text = txtUUID.Text
            End Using
        Catch validatioFault As FaultException(Of SrvTimbrado.FallaValidacion)
            Using errorForm = New frmError(TranslateFault.ToValidationError(validatioFault.Detail))
                errorForm.ShowDialog()
            End Using
        Catch serviceFault As FaultException(Of SrvTimbrado.FallaServicio)
            Using errorForm = New frmError(TranslateFault.ToServiceError(serviceFault.Detail))
                errorForm.ShowDialog()
            End Using
        Catch sessionFauld As FaultException(Of SrvTimbrado.FallaSesion)
            Using errorForm = New frmError(TranslateFault.ToSessionError(sessionFauld.Detail))
                errorForm.ShowDialog()
            End Using
        Catch ex As Exception
            ShowUnknownError(ex.Message)

        End Try
    End Sub

    Public Sub ShowUnknownError(ByVal message As String)
        MessageBox.Show([String].Format("Error: {0}", message), "Ecodex Cancela Documento")
    End Sub
End Class